home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / Install / program files / Borland / BDS / 3.0 / Demos / CSharp / Applications / Threads / ThrdDemo.cs < prev    next >
Encoding:
Text File  |  2004-10-22  |  11.8 KB  |  339 lines

  1. /*********************************************************
  2. C# Threads demo
  3. Example submitted by Chua Chee Wee, Singapore
  4. **********************************************************/
  5. using System;
  6. using System.Threading;
  7. using System.Drawing;
  8. using System.Runtime.InteropServices;
  9.  
  10. namespace ThrdDemo {
  11.  
  12.  
  13.   public class ThreadSortForm : System.Windows.Forms.Form {
  14.     public delegate void ThreadDoneDelegate();
  15.     public delegate void VisualSwapDelegate();
  16.  
  17.       class TSortHandler {
  18.         private int[] FSortArray;
  19.         protected System.Windows.Forms.Panel FPanel;
  20.         protected int FI, FA, FJ, FB;
  21.         protected event VisualSwapDelegate FDoVisualSwap;
  22.         Pen pen;
  23.         protected void PaintLine(System.Drawing.Graphics Canvas, int I, int Len) {
  24.          Canvas.DrawLine(pen, 0, I*2+1, Len, I*2+1);
  25.         }
  26.         protected void DoVisualSwap() {
  27.          System.Drawing.Graphics Canvas = FPanel.CreateGraphics();
  28.  
  29.          // erase the line, by painting over it with the
  30.          // background color
  31.          pen = new Pen(FPanel.BackColor, 1);
  32.          PaintLine(Canvas, FI, FA);
  33.          PaintLine(Canvas, FJ, FB);
  34.  
  35.          pen = new Pen(Color.Red, 1);
  36.          PaintLine(Canvas, FI, FB);
  37.          PaintLine(Canvas, FJ, FA);
  38.  
  39.          Canvas.Dispose();
  40.         }
  41.  
  42.         protected void VisualSwap(int A, int B, int I, int J) {
  43.          FA = A;
  44.          FB = B;
  45.          FI = I;
  46.          FJ = J;
  47.          if (FDoVisualSwap!=null)
  48.           FPanel.Invoke(FDoVisualSwap);
  49.         }
  50.  
  51.         public event ThreadDoneDelegate OnThreadDone;
  52.  
  53.  
  54.         public void Execute() {
  55.          Sort(FSortArray);
  56.          if (OnThreadDone!=null)
  57.            OnThreadDone();
  58.         }
  59.  
  60.         public TSortHandler(System.Windows.Forms.Panel Panel, int[] SortArray) {
  61.          FPanel = Panel;
  62.  // The assignment below is assignment by reference
  63.          FSortArray = SortArray;
  64.          FDoVisualSwap += new VisualSwapDelegate(this.DoVisualSwap);
  65.         }
  66.  
  67.         public virtual void Sort(int[] A) {
  68.         }
  69.     }
  70.  
  71.     class TBubbleSortHandler : TSortHandler {
  72.         public TBubbleSortHandler(System.Windows.Forms.Panel Panel,
  73.                                   int[] SortArray): base(Panel, SortArray) {
  74.         }
  75.  
  76.         public override void Sort(int[] A) {
  77.  
  78.          for(int I = A.GetUpperBound(0);I>=A.GetLowerBound(0); I--)
  79.            for(int J = A.GetLowerBound(0); J<A.GetUpperBound(0); J++)
  80.              if (A[J] > A[J + 1])
  81.              {
  82.                VisualSwap(A[J], A[J + 1], J, J + 1);
  83.                int T = A[J];
  84.                A[J] = A[J + 1];
  85.                A[J + 1] = T;
  86. // superfluous code, but shows how to check if thread has been terminated
  87. //               if (Thread.CurrentThread.ThreadState==ThreadState.Stopped) return;
  88.              }
  89.        }
  90.     }
  91.  
  92.     class TSelectionSortHandler : TSortHandler {
  93.         public TSelectionSortHandler(System.Windows.Forms.Panel Panel,
  94.                                      int[] SortArray): base(Panel, SortArray) {
  95.         }
  96.  
  97.         public override void Sort(int[] A) {
  98.  
  99.          for(int I = A.GetLowerBound(0); I<A.GetUpperBound(0); I++)
  100.            for(int J = A.GetUpperBound(0); J>=I+1; J--)
  101.              if (A[I] > A[J])
  102.              {
  103.                VisualSwap(A[I], A[J], I, J);
  104.                int T = A[I];
  105.                A[I] = A[J];
  106.                A[J] = T;
  107. // superfluous code, but shows how to check if thread has been terminated
  108. //               if (Thread.CurrentThread.ThreadState==ThreadState.Stopped) return;
  109.              }
  110.         }
  111.     }
  112.  
  113.     class TQuickSortHandler : TSortHandler {
  114.         public TQuickSortHandler(System.Windows.Forms.Panel Panel,
  115.                                  int[] SortArray): base(Panel, SortArray) {
  116.         }
  117.  
  118.         private void QuickSort(int[] A, int iLo, int iHi) {
  119.  
  120.             int Lo = iLo;
  121.             int Hi = iHi;
  122.             int Mid = A[(Lo + Hi) / 2];
  123.             do {
  124.               while (A[Lo] < Mid) Lo++;
  125.               while (A[Hi] > Mid) Hi--;
  126.               if (Lo <= Hi) {
  127.                 VisualSwap(A[Lo], A[Hi], Lo, Hi);
  128.                 int T = A[Lo];
  129.                 A[Lo] = A[Hi];
  130.                 A[Hi] = T;
  131.                 Lo++;
  132.                 Hi--;
  133.               }
  134.             } while (Lo <= Hi);
  135.             if (Hi > iLo) QuickSort(A, iLo, Hi);
  136.             if (Lo < iHi) QuickSort(A, Lo, iHi);
  137. // superfluous code, but shows how to check if thread has been terminated
  138. //               if (Thread.CurrentThread.ThreadState==ThreadState.Stopped) return;
  139.         }
  140.  
  141.         public override void Sort(int[] A) {
  142.          QuickSort(A, A.GetLowerBound(0), A.GetUpperBound(0));
  143.         }
  144.     }
  145.  
  146.     public ThreadSortForm() {
  147.         this.InitializeComponent();
  148.         BubbleSortArray = new int[115];
  149.         QuickSortArray = new int[115];
  150.         SelectionSortArray = new int[115];
  151.         RandomizeArrays();
  152.     }
  153.  
  154.     public static void Main() {
  155.         System.Windows.Forms.Application.Run(new ThreadSortForm());
  156.     }
  157.  
  158.         private void InitializeComponent()
  159.         {
  160.             this.PanelBubbleSort = new System.Windows.Forms.Panel();
  161.             this.PanelSelectionSort = new System.Windows.Forms.Panel();
  162.             this.PanelQuickSort = new System.Windows.Forms.Panel();
  163.             this.btnSort = new System.Windows.Forms.Button();
  164.             this.BubbleSort = new System.Windows.Forms.Label();
  165.             this.SelectionSort = new System.Windows.Forms.Label();
  166.             this.QuickSort = new System.Windows.Forms.Label();
  167.             this.SuspendLayout();
  168.             // 
  169.             // PanelBubbleSort
  170.             // 
  171.             this.PanelBubbleSort.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
  172.             this.PanelBubbleSort.Location = new System.Drawing.Point(8, 24);
  173.             this.PanelBubbleSort.Name = "PanelBubbleSort";
  174.             this.PanelBubbleSort.Size = new System.Drawing.Size(177, 233);
  175.             this.PanelBubbleSort.TabIndex = 0;
  176.             this.PanelBubbleSort.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
  177.             // 
  178.             // PanelSelectionSort
  179.             // 
  180.             this.PanelSelectionSort.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
  181.             this.PanelSelectionSort.Location = new System.Drawing.Point(192, 24);
  182.             this.PanelSelectionSort.Name = "PanelSelectionSort";
  183.             this.PanelSelectionSort.Size = new System.Drawing.Size(177, 233);
  184.             this.PanelSelectionSort.TabIndex = 1;
  185.             this.PanelSelectionSort.Paint += new System.Windows.Forms.PaintEventHandler(this.panel2_Paint);
  186.             // 
  187.             // PanelQuickSort
  188.             // 
  189.             this.PanelQuickSort.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
  190.             this.PanelQuickSort.Location = new System.Drawing.Point(376, 24);
  191.             this.PanelQuickSort.Name = "PanelQuickSort";
  192.             this.PanelQuickSort.Size = new System.Drawing.Size(177, 233);
  193.             this.PanelQuickSort.TabIndex = 2;
  194.             this.PanelQuickSort.Paint += new System.Windows.Forms.PaintEventHandler(this.panel3_Paint);
  195.             // 
  196.             // btnSort
  197.             // 
  198.             this.btnSort.Location = new System.Drawing.Point(416, 264);
  199.             this.btnSort.Name = "btnSort";
  200.             this.btnSort.Size = new System.Drawing.Size(136, 25);
  201.             this.btnSort.TabIndex = 3;
  202.             this.btnSort.Text = "Start sorting";
  203.             this.btnSort.Click += new System.EventHandler(this.btnSort_Click);
  204.             // 
  205.             // BubbleSort
  206.             // 
  207.             this.BubbleSort.Location = new System.Drawing.Point(8, 8);
  208.             this.BubbleSort.Name = "BubbleSort";
  209.             this.BubbleSort.Size = new System.Drawing.Size(128, 13);
  210.             this.BubbleSort.TabIndex = 4;
  211.             this.BubbleSort.Text = "Bubble Sort";
  212.             // 
  213.             // SelectionSort
  214.             // 
  215.             this.SelectionSort.Location = new System.Drawing.Point(192, 8);
  216.             this.SelectionSort.Name = "SelectionSort";
  217.             this.SelectionSort.Size = new System.Drawing.Size(136, 13);
  218.             this.SelectionSort.TabIndex = 5;
  219.             this.SelectionSort.Text = "Selection Sort";
  220.             // 
  221.             // QuickSort
  222.             // 
  223.             this.QuickSort.Location = new System.Drawing.Point(376, 8);
  224.             this.QuickSort.Name = "QuickSort";
  225.             this.QuickSort.Size = new System.Drawing.Size(128, 13);
  226.             this.QuickSort.TabIndex = 6;
  227.             this.QuickSort.Text = "Quick Sort";
  228.             // 
  229.             // ThreadSortForm
  230.             // 
  231.             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  232.             this.ClientSize = new System.Drawing.Size(562, 295);
  233.             this.Controls.Add(this.SelectionSort);
  234.             this.Controls.Add(this.BubbleSort);
  235.             this.Controls.Add(this.btnSort);
  236.             this.Controls.Add(this.PanelQuickSort);
  237.             this.Controls.Add(this.PanelSelectionSort);
  238.             this.Controls.Add(this.PanelBubbleSort);
  239.             this.Controls.Add(this.QuickSort);
  240.             this.Name = "ThreadSortForm";
  241.             this.Text = "Thread Sorting Demo";
  242.             this.ResumeLayout(false);
  243.         }
  244.         private System.Windows.Forms.Panel PanelBubbleSort;
  245.         private System.Windows.Forms.Panel PanelSelectionSort;
  246.         private System.Windows.Forms.Panel PanelQuickSort;
  247.         private System.Windows.Forms.Button btnSort;
  248.         private System.Windows.Forms.Label BubbleSort;
  249.         private System.Windows.Forms.Label SelectionSort;
  250.         private System.Windows.Forms.Label QuickSort;
  251.  
  252.    private int[] BubbleSortArray, SelectionSortArray, QuickSortArray;
  253.  
  254.    private System.Random Random;
  255.    private int ThreadsRunning;
  256.  
  257.  // It is possible that ThreadDone is called by a thread while it is executing,
  258.  // and that, they're both decrementing the ThreadsRunning variable at the
  259.  // same time. locking it will ensure that ThreadsRunning is not decremented
  260.  // at the same time. Highly unlikely that ThreadDone will be called at
  261.  // the same time, since the sorting algorithms' speed varies greatly.
  262.  // However, it is good practice to lock a block of code that is likely
  263.  // to be executed at the same time, ie, make no assumptions when threads
  264.  // are involved!!! 
  265.    public void ThreadDone() {
  266.     lock(this) {
  267.      if (--ThreadsRunning==0) {
  268.       btnSort.Enabled = true;
  269.      }
  270.     }
  271.    }
  272.  
  273.  // Randomizes the 3 arrays with random seed from the current time
  274.    private void RandomizeArrays() {
  275.  
  276.      Random = new System.Random(unchecked((int)DateTime.Now.Ticks));
  277.      for (int I=0;I<BubbleSortArray.Length;I++){
  278.       BubbleSortArray[I] = Random.Next(170);
  279.      }
  280.      System.Array.Copy(BubbleSortArray, QuickSortArray, BubbleSortArray.Length);
  281.      System.Array.Copy(BubbleSortArray, SelectionSortArray, BubbleSortArray.Length);
  282.    }
  283.  
  284.    private void btnSort_Click(object sender, System.EventArgs e) {
  285.     RandomizeArrays();
  286.     Refresh();
  287.     ThreadsRunning = 3;
  288.     btnSort.Enabled = false;
  289.  
  290.     TSelectionSortHandler SelectionSortHandler = new TSelectionSortHandler(PanelSelectionSort, SelectionSortArray);
  291.     SelectionSortHandler.OnThreadDone += new ThreadDoneDelegate(this.ThreadDone);
  292.  
  293.     TQuickSortHandler QuickSortHandler = new TQuickSortHandler(PanelQuickSort, QuickSortArray);
  294.     QuickSortHandler.OnThreadDone += new ThreadDoneDelegate(this.ThreadDone);
  295.  
  296.     TBubbleSortHandler BubbleSortHandler = new TBubbleSortHandler(PanelBubbleSort, BubbleSortArray);
  297.     BubbleSortHandler.OnThreadDone += new ThreadDoneDelegate(this.ThreadDone);
  298.  
  299.     Thread SelectionSortThread = new Thread(new ThreadStart(SelectionSortHandler.Execute));
  300.     SelectionSortThread.Name = "Selection Sort";
  301.     SelectionSortThread.IsBackground = true;
  302.  
  303.     Thread QuickSortThread = new Thread(new ThreadStart(QuickSortHandler.Execute));
  304.     QuickSortThread.Name = "Quick Sort";
  305.     QuickSortThread.IsBackground = true;
  306.  
  307.     Thread BubbleSortThread = new Thread(new ThreadStart(BubbleSortHandler.Execute));
  308.     BubbleSortThread.Name = "Bubble Sort";
  309.     BubbleSortThread.IsBackground = true;
  310.  
  311.     SelectionSortThread.Start();
  312.     QuickSortThread.Start();
  313.     BubbleSortThread.Start();
  314.  
  315.    }
  316.  
  317.    private void PaintArray(System.Drawing.Graphics canvas, int[] A) {
  318.     Pen redPen = new Pen(Color.Red, 1);
  319.     for (int I=0;I<A.Length;I++) {
  320.      canvas.DrawLine(redPen, 0, I*2+1, A[I], I*2+1);
  321.     }
  322.    }
  323.  
  324.    private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) {
  325.     PaintArray(e.Graphics, BubbleSortArray);
  326.    }
  327.  
  328.    private void panel2_Paint(object sender, System.Windows.Forms.PaintEventArgs e) {
  329.     PaintArray(e.Graphics, SelectionSortArray);
  330.    }
  331.  
  332.    private void panel3_Paint(object sender, System.Windows.Forms.PaintEventArgs e) {
  333.     PaintArray(e.Graphics, QuickSortArray);
  334.    }
  335.  
  336.   }
  337. }
  338.  
  339.